﻿using System.Net;
using System.Text;

namespace rpc_service.test;

public class TestMethod
{
    public string GetToken()
    {
        return "asfdsaf";
    }

    public static void SendHttpPostRequest(string url, string postData)
    {
        // 创建 WebRequest 对象
        WebRequest request = WebRequest.Create(url);
        request.Method = "POST";

        // 设置 Content-Type 为 application/json
        request.ContentType = "application/json";

        // 将字符串转换为字节数组
        byte[] postDataBytes = Encoding.UTF8.GetBytes(postData);

        // 设置 Content-Length
        request.ContentLength = postDataBytes.Length;

        // 获取请求流并写入数据
        Stream requestStream = request.GetRequestStream();
        requestStream.Write(postDataBytes, 0, postDataBytes.Length);
        requestStream.Close();

        // 获取响应
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        Stream responseStream = response.GetResponseStream();

        // 读取响应内容
        StreamReader reader = new StreamReader(responseStream);
        string responseContent = reader.ReadToEnd();

        // 关闭资源
        reader.Close();
        responseStream.Close();
        response.Close();

        Console.WriteLine("Response Content: " + responseContent);
    }
}